/****************************************************************************
 *   $Id:: timer32.c 3635 2010-06-02 00:31:46Z usb00423                     $
 *   Project: NXP LPC11xx 32-bit timer example
 *
 *   Description:
 *     This file contains 32-bit timer code example which include timer 
 *     initialization, timer interrupt handler, and related APIs for 
 *     timer setup.
 *
 ****************************************************************************
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * products. This software is supplied "AS IS" without any warranties.
 * NXP Semiconductors assumes no responsibility or liability for the
 * use of the software, conveys no license or title under any patent,
 * copyright, or mask work right to the product. NXP Semiconductors
 * reserves the right to make changes in the software without
 * notification. NXP Semiconductors also make no representation or
 * warranty that such application will be suitable for the specified
 * use without further testing or modification.
****************************************************************************/
#include "LPC11xx.h"
#include "timer.h"

volatile uint32_t timer32_0_counter = 0;
volatile uint32_t timer32_1_counter = 0;
volatile uint32_t timer32_0_capture = 0;
volatile uint32_t timer32_1_capture = 0;
volatile uint32_t timer32_0_period = 0;
volatile uint32_t timer32_1_period = 0;

volatile uint32_t timer16_0_counter = 0;
volatile uint32_t timer16_1_counter = 0;
volatile uint32_t timer16_0_capture = 0;
volatile uint32_t timer16_1_capture = 0;
volatile uint32_t timer16_0_period = 0;
volatile uint32_t timer16_1_period = 0;


// the call-back function pointer.
callback_void timer16_0_int_func = 0;
callback_void timer16_1_int_func = 0;

callback_void timer32_0_int_func = 0;
callback_void timer32_1_int_func = 0;

/*****************************************************************************
** Function name:		TimerDelayMs
**
** Descriptions:		Start the timer delay in milo seconds
**				until elapsed
**
** parameters:		timer number, Delay value in milo second			 
** 						
** Returned value:	None
** 
*****************************************************************************/
void TimerDelayMs(LPC_TMR_TypeDef * const timer, uint32_t delayInMs)
{
    if (delayInMs == 0)
        return;

    /* setup timer #0 for delay */
    timer->TCR = 0x02;		/* reset timer */
    timer->PR  = 0x00;		/* set prescaler to zero */
    timer->MR0 = delayInMs * (SystemAHBFrequency / 1000);
    timer->IR  = 0xff;		/* reset all interrrupts */
    timer->MCR = 0x04;		/* stop timer on match */
    timer->TCR = 0x01;		/* start timer */
  
    /* wait until delay time has elapsed */
    while (timer->TCR & 0x01);
    
    return;
}

/*****************************************************************************
** Function name:		TimerDelayUs
**
** Descriptions:		Start the timer delay in us
**				until elapsed
**
** parameters:		timer number, Delay value in us			 
** 						
** Returned value:	None
** 
*****************************************************************************/
void TimerDelayUs(LPC_TMR_TypeDef * const timer, uint32_t delayInUs)
{
    if (delayInUs == 0)
        return;
    
    /* setup timer #0 for delay */
    timer->TCR = 0x02;		/* reset timer */
    timer->PR  = 0x00;		/* set prescaler to zero */
    timer->MR0 = delayInUs * (SystemAHBFrequency / 1000000);
    timer->IR  = 0xff;		/* reset all interrrupts */
    timer->MCR = 0x04;		/* stop timer on match */
    timer->TCR = 0x01;		/* start timer */
  
    /* wait until delay time has elapsed */
    while (timer->TCR & 0x01);

    return;
}

/******************************************************************************
** Function name:		TIMER_0_IRQHandler
**
** Descriptions:		Timer/Counter 0 interrupt handler
**						executes each 10ms @ 60 MHz CPU Clock
**
** parameters:			None
** Returned value:		None
** 
******************************************************************************/
void TIMER16_0_IRQHandler(void)
{
  uint32_t regVal;
  regVal = LPC_TMR16B0->IR;
    
  if ( LPC_TMR16B0->IR & 0x1 )
  {
	LPC_TMR16B0->IR = 1;			/* clear interrupt flag */
	timer16_0_counter++;
  }
  if ( LPC_TMR16B0->IR & (0x1<<4) )
  {
	LPC_TMR16B0->IR = 0x1<<4;		/* clear interrupt flag */
	timer16_0_capture++;
  }

  if (timer16_0_int_func != 0) {
    timer16_0_int_func((void*)&regVal);
  }
  return;
}

/******************************************************************************
** Function name:		TIMER_1_IRQHandler
**
** Descriptions:		Timer/Counter 1 interrupt handler
**						executes each 10ms @ 60 MHz CPU Clock
**
** parameters:			None
** Returned value:		None
** 
******************************************************************************/
void TIMER16_1_IRQHandler(void)
{
  uint32_t regVal;
  regVal = LPC_TMR16B1->IR;

  if ( LPC_TMR16B1->IR & 0x1 )
  {  
	LPC_TMR16B1->IR = 1;			/* clear interrupt flag */
	timer16_1_counter++;
  }
  if ( LPC_TMR16B1->IR & (0x1<<4) )
  {
	LPC_TMR16B1->IR = 0x1<<4;		/* clear interrupt flag */
	timer16_1_capture++;
  }

  if (timer16_1_int_func != 0) {
    timer16_1_int_func((void*)&regVal);
  }
  return;
}

/******************************************************************************
** Function name:		TIMER32_0_IRQHandler
**
** Descriptions:		Timer/Counter 0 interrupt handler
**				executes each 10ms @ 60 MHz CPU Clock
**
** parameters:		None
** Returned value:	None
** 
******************************************************************************/
void TIMER32_0_IRQHandler(void)
{
    uint32_t regVal;
    regVal = LPC_TMR32B0->IR;

    if ( LPC_TMR32B0->IR & 0x01 )
    {  
        LPC_TMR32B0->IR = 1;				/* clear interrupt flag */
        timer32_0_counter++;
    }
    if ( LPC_TMR32B0->IR & (0x1<<4) )
    {  
        LPC_TMR32B0->IR = 0x1<<4;			/* clear interrupt flag */
        timer32_0_capture++;
    }
    
    if (timer32_0_int_func != 0) {
        timer32_0_int_func((void*)&regVal);
    }
  return;
}

/******************************************************************************
** Function name:		TIMER32_1_IRQHandler
**
** Descriptions:		Timer/Counter 1 interrupt handler
**				executes each 10ms @ 60 MHz CPU Clock
**
** parameters:		None
** Returned value:	None
** 
******************************************************************************/
void TIMER32_1_IRQHandler(void)
{
    uint32_t regVal;
    regVal = LPC_TMR32B1->IR;
    
    if ( LPC_TMR32B1->IR & 0x01 )
    {
        LPC_TMR32B1->IR = 1;            /* clear interrupt flag */
        timer32_1_counter++;
    }
    if ( LPC_TMR32B1->IR & (0x1<<4) )
    {
        LPC_TMR32B1->IR = 0x1<<4;           /* clear interrupt flag */
        timer32_1_capture++;
    }
    
    if (timer32_1_int_func != 0) {
        timer32_1_int_func((void*)&regVal);
    }
    
    return;
}

/******************************************************************************
** Function name:		TimerInit
**
** Descriptions:		Initialize timer, set timer interval, reset timer,
**				install timer interrupt handler
**
** parameters:		timer number and timer interval
** Returned value:	None
** 
******************************************************************************/
void TimerInit(LPC_TMR_TypeDef * const timer) 
{
    if (timer == LPC_TMR16B0) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
        LPC_IOCON->PIO0_2           &= ~0x07;	/*  Timer0_16 I/O config */
        LPC_IOCON->PIO0_2           |= 0x02;		/* Timer0_16 CAP0 */
        LPC_IOCON->PIO0_8           &= ~0x07;	
        LPC_IOCON->PIO0_8           |= 0x02;		/* Timer0_16 MAT0 */
        LPC_IOCON->PIO0_9           &= ~0x07;
        LPC_IOCON->PIO0_9           |= 0x02;		/* Timer0_16 MAT1 */
    
#ifdef __JTAG_DISABLED
        LPC_IOCON->JTAG_TCK_PIO0_10 &= ~0x07;
        LPC_IOCON->JTAG_TCK_PIO0_10 |= 0x03;		/* Timer0_16 MAT2 */
#endif	
    		
        timer16_0_counter = 0;
        timer16_0_capture = 0;
        
        /* Enable the TIMER0 Interrupt */
        NVIC_EnableIRQ(TIMER_16_0_IRQn);
        NVIC_SetPriority(TIMER_16_0_IRQn, 2);
    
        /* set the call back interrupt function to null. */
        timer16_0_int_func = 0;
    }
    else if (timer == LPC_TMR16B1) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
        LPC_IOCON->PIO1_8           &= ~0x07;	/*  Timer1_16 I/O config */
        LPC_IOCON->PIO1_8           |= 0x01;		/* Timer1_16 CAP0 */
        LPC_IOCON->PIO1_9           &= ~0x07;	
        LPC_IOCON->PIO1_9           |= 0x01;		/* Timer1_16 MAT0 */
        LPC_IOCON->PIO1_10          &= ~0x07;
        LPC_IOCON->PIO1_10          |= 0x02;		/* Timer1_16 MAT1 */	
    		
        timer16_1_counter = 0;
        timer16_1_capture = 0;
        
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_1_IRQn);
        NVIC_SetPriority(TIMER_16_1_IRQn, 2);
    
        /* set the call back interrupt function to null. */
        timer16_1_int_func = 0;
    }
    else if (timer == LPC_TMR32B0) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
        LPC_IOCON->PIO1_5 &= ~0x07;	/*  Timer0_32 I/O config */
        LPC_IOCON->PIO1_5 |= 0x02;	/* Timer0_32 CAP0 */
        LPC_IOCON->PIO1_6 &= ~0x07;
        LPC_IOCON->PIO1_6 |= 0x02;	/* Timer0_32 MAT0 */
        LPC_IOCON->PIO1_7 &= ~0x07;
        LPC_IOCON->PIO1_7 |= 0x02;	/* Timer0_32 MAT1 */
        LPC_IOCON->PIO0_1 &= ~0x07;	
        LPC_IOCON->PIO0_1 |= 0x02;	/* Timer0_32 MAT2 */
#ifdef __JTAG_DISABLED
        LPC_IOCON->R_PIO0_11 &= ~0x07;	
        LPC_IOCON->R_PIO0_11 |= 0x03;	/* Timer0_32 MAT3 */
#endif
    
        timer32_0_counter = 0;
        timer32_0_capture = 0;
    
        /* Enable the TIMER0 Interrupt */
        NVIC_EnableIRQ(TIMER_32_0_IRQn);
    
        /* set the call back interrupt function to null. */
        timer32_0_int_func = 0;
    }
    else if (timer == LPC_TMR32B1) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
#ifdef __JTAG_DISABLED
        LPC_IOCON->R_PIO1_0  &= ~0x07;	/*  Timer1_32 I/O config */
        LPC_IOCON->R_PIO1_0  |= 0x03;	/* Timer1_32 CAP0 */
        LPC_IOCON->R_PIO1_1  &= ~0x07;	
        LPC_IOCON->R_PIO1_1  |= 0x03;	/* Timer1_32 MAT0 */
        LPC_IOCON->R_PIO1_2 &= ~0x07;
        LPC_IOCON->R_PIO1_2 |= 0x03;	/* Timer1_32 MAT1 */
        LPC_IOCON->SWDIO_PIO1_3  &= ~0x07;
        LPC_IOCON->SWDIO_PIO1_3  |= 0x03;	/* Timer1_32 MAT2 */
#endif
        LPC_IOCON->PIO1_4 &= ~0x07;
        LPC_IOCON->PIO1_4 |= 0x02;		/* Timer0_32 MAT3 */
    
        timer32_1_counter = 0;
        timer32_1_capture = 0;
    
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_32_1_IRQn);
    
        /* set the call back interrupt function to null. */
        timer32_1_int_func = 0;
    }
    else {
        return;
    }
  
#if TIMER_MATCH
	timer->EMR &= ~(0xFF<<4);
	timer->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8)|(0x3<<10));	/* MR0/1/2/3 Toggle */
#else
	/* Capture 0 on falling edge, interrupt enable. */
	timer->CCR = (0x2<<0)|(0x1<<2);
#endif
    timer->MCR = 3;			/* Interrupt and Reset on MR0 */
    
    return;
}
/******************************************************************************
** Function name:		TimerInitPWM
**
** Descriptions:		Initialize timer as PWM
**
** parameters:		timer number, period and match enable:
**				match_enable[0] = PWM for MAT0 
**				match_enable[1] = PWM for MAT1
**				match_enable[2] = PWM for MAT2
** Returned value:	None
** 
******************************************************************************/
void TimerInitPWM(LPC_TMR_TypeDef * const timer, uint32_t period, uint8_t match_enable)
{
    TimerDisable(timer);
    
    if (timer == LPC_TMR16B0) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
    		
        /* Setup the external match register */
        LPC_TMR16B0->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable);
    		
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        if (match_enable & 0x01)
        {
          LPC_IOCON->PIO0_8           &= ~0x07;	
          LPC_IOCON->PIO0_8           |= 0x02;		/* Timer0_16 MAT0 			*/
        }
        /* If match1 is enabled, set the output */
        if (match_enable & 0x02)
        {
          LPC_IOCON->PIO0_9           &= ~0x07;
          LPC_IOCON->PIO0_9           |= 0x02;		/* Timer0_16 MAT1 			*/
        }
        /* If match2 is enabled, set the output */
        if (match_enable & 0x04)
        {
          LPC_IOCON->SWCLK_PIO0_10 &= ~0x07;
          LPC_IOCON->SWCLK_PIO0_10 |= 0x03;		/* Timer0_16 MAT2 */
        }

        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_0_IRQn);
    }
    else if (timer == LPC_TMR16B1) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
    		
        /* Setup the external match register */
        LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
    		
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        if (match_enable & 0x01)
        {
          LPC_IOCON->PIO1_9           &= ~0x07;	
          LPC_IOCON->PIO1_9           |= 0x01;		/* Timer1_16 MAT0 */
        }
        /* If match1 is enabled, set the output */
        if (match_enable & 0x02)
        {
          LPC_IOCON->PIO1_10          &= ~0x07;
          LPC_IOCON->PIO1_10          |= 0x02;		/* Timer1_16 MAT1 */
        }
        
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_1_IRQn);
    }
    else if (timer == LPC_TMR32B0) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
    
        /* Setup the external match register */
        LPC_TMR32B0->EMR = (1<<EMC3)|(2<<EMC2)|(1<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable);
     
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        if (match_enable & 0x01)
        {
    //	 	LPC_IOCON->PIO1_6           &= ~0x07;
    //	  	LPC_IOCON->PIO1_6           |= 0x02;		/* Timer0_32 MAT0 */
        }
        /* If match1 is enabled, set the output */
        if (match_enable & 0x02)
        {
          LPC_IOCON->PIO1_7           &= ~0x07;
          LPC_IOCON->PIO1_7           |= 0x02;		/* Timer0_32 MAT1 */
        }
        /* If match2 is enabled, set the output */
        if (match_enable & 0x04)
        {
          LPC_IOCON->PIO0_1           &= ~0x07;	
          LPC_IOCON->PIO0_1           |= 0x02;		/* Timer0_32 MAT2 */
        }
        /* If match3 is enabled, set the output */
        if (match_enable & 0x08)
        {
          LPC_IOCON->R_PIO0_11 &= ~0x07;	
          LPC_IOCON->R_PIO0_11 |= 0x03;		/* Timer0_32 MAT3 */
        }
    }
    else if (timer == LPC_TMR32B1) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
    
        /* Setup the external match register */
        LPC_TMR32B1->EMR = (1<<EMC3)|(1<<EMC2)|(2<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable);
    
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        if (match_enable & 0x01)
        {
          LPC_IOCON->R_PIO1_1  &= ~0x07;	
          LPC_IOCON->R_PIO1_1  |= 0x03;		/* Timer1_32 MAT0 */
        }
        /* If match1 is enabled, set the output */
        if (match_enable & 0x02)
        {
          LPC_IOCON->R_PIO1_2 &= ~0x07;
          LPC_IOCON->R_PIO1_2 |= 0x03;		/* Timer1_32 MAT1 */
        }
        /* If match2 is enabled, set the output */
        if (match_enable & 0x04)
        {
          LPC_IOCON->SWDIO_PIO1_3   &= ~0x07;
          LPC_IOCON->SWDIO_PIO1_3   |= 0x03;		/* Timer1_32 MAT2 */
        }
        /* If match3 is enabled, set the output */
        if (match_enable & 0x08)
        {
          LPC_IOCON->PIO1_4           &= ~0x07;
          LPC_IOCON->PIO1_4           |= 0x02;		/* Timer1_32 MAT3 */
        }
    }
    else {
        return;
    }

    /* Enable the selected PWMs and enable Match3 */
    timer->PWMC = (1<<3)|(match_enable);

    /* Setup the match registers */
    /* set the period value to a global variable */
    timer32_0_period = period;
    timer->MR3 = timer32_0_period;
    timer->MR0	= timer32_0_period;	///2;
    timer->MR1	= timer32_0_period/2;
    timer->MR2	= timer32_0_period/2;

    timer->MCR = 1<<10;				/* Reset on MR3 */
}

void TimerSetIntCallback(LPC_TMR_TypeDef * const timer, callback_void func)
{
    if (timer == LPC_TMR16B0) {
        timer16_0_int_func = func;
    }
    else if (timer == LPC_TMR16B1) {
        timer16_1_int_func = func;
    }
    else if (timer == LPC_TMR32B0) {
        timer32_0_int_func = func;
    }
    else if (timer == LPC_TMR32B1) {
        timer32_1_int_func = func;
    }
}

/******************************************************************************
** Function name:		TimerEnable
**
** Descriptions:		Enable timer
**
** parameters:		timer number: 0 or 1
** Returned value:	None
** 
******************************************************************************/
void TimerEnable(LPC_TMR_TypeDef * const timer)
{
    timer->TCR = 1;
    
    return;
}

/******************************************************************************
** Function name:		TimerDisable
**
** Descriptions:		Disable timer
**
** parameters:		timer number: 0 or 1
** Returned value:	None
** 
******************************************************************************/
void TimerDisable(LPC_TMR_TypeDef * const timer)
{
    timer->TCR = 0;
    return;
}

/******************************************************************************
** Function name:		TimerSetTime
**
** Descriptions:		set time for timer.
**
** parameters:		timer number: 0 or 1, time for timer.
** Returned value:	None
** 
******************************************************************************/
void TimerSetTime(LPC_TMR_TypeDef * const timer, uint32_t timerInterval)
{
    timer->MR0 = timerInterval;
    
    return;
}

/******************************************************************************
** Function name:		TimerClear
**
** Descriptions:		Clear timer
**
** parameters:		timer number: 0 or 1
** Returned value:	None
** 
******************************************************************************/
void TimerClear(LPC_TMR_TypeDef * const timer)
{
  uint32_t regVal;

    regVal = timer->TCR;
    regVal |= 0x02;
    timer->TCR = regVal;
    
    return;
}

/******************************************************************************
** Function name:		TimerSetMatchPWM
**
** Descriptions:		Set the pwm32 match values
**
** parameters:		timer number, match numner and the value
**
** Returned value:	None
** 
******************************************************************************/
void TimerSetMatchPWM (LPC_TMR_TypeDef * const timer, uint8_t match_nr, uint32_t value)
{
    switch (match_nr)
    {
    case 0:
        timer->MR0 = value;
        break;
    case 1: 
        timer->MR1 = value;
        break;
    case 2:
        timer->MR2 = value;
        break;
    case 3: 
        timer->MR3 = value;
        break;
    default:
        break;
    }
}

void TimerInitCounter(LPC_TMR_TypeDef * const timer)
{
    if (timer == LPC_TMR16B0) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
        // notice the P0.2 is SSEL0 also.
        LPC_IOCON->PIO0_2 &= ~0x07;						/*  Timer1_16 I/O config */
        LPC_IOCON->PIO0_2 |= 0x01 | (2<<3);				/* Timer1_16 CAP0 */

        timer16_0_counter = 0;
        timer32_0_capture = 0;
    }
    else if (timer == LPC_TMR16B1) {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
        LPC_IOCON->PIO1_8 &= ~0x07;						/*  Timer1_16 I/O config */
        LPC_IOCON->PIO1_8 |= 0x01 | (2<<3);				/* Timer1_16 CAP0 */

        timer16_1_counter = 0;
        timer32_1_capture = 0;
    }
    else if ( timer == LPC_TMR32B0 ) {
        /* Some of the I/O pins need to be clearfully planned if
           you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9);
        LPC_IOCON->PIO1_5 &= ~0x07;	/*  Timer0_32 I/O config */
        LPC_IOCON->PIO1_5 |= 0x02;	/* Timer0_32 CAP0 */

        timer32_0_counter = 0;
        timer32_0_capture = 0;

        /* set the call back interrupt function to null. */
        timer32_0_int_func = 0;
    }
    else if ( timer == LPC_TMR32B1 )
    {
        /* Some of the I/O pins need to be clearfully planned if
           you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
#ifdef __JTAG_DISABLED
        LPC_IOCON->R_PIO1_0  &= ~0x07;	/*  Timer1_32 I/O config */
        LPC_IOCON->R_PIO1_0  |= 0x03;	/* Timer1_32 CAP0 */
#endif

        timer32_1_counter = 0;
        timer32_1_capture = 0;

        /* set the call back interrupt function to null. */
        timer32_1_int_func = 0;
    }
    
	/* CCR must set to 0 when using counter mode. */
	timer->CCR = 0x00;
    
    timer->CTCR = 0x1;    /* counting on CAP rising edge. */
    
  return;
}


/******************************************************************************
**                            End Of File
******************************************************************************/
